package com.lenovo.testreport.online.web.smoke.service;

import com.lenovo.testreport.online.web.smoke.mapper.OnlineWebSmokeReportMapper;
import com.lenovo.testreport.online.web.smoke.model.OnlineWebSmokeReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author CJF
 * @date 2018/10
 * @version Updated at 2019/05/24 By Howie Lu
 */
@Service("OnlineWebSmokeReportService")
public class OnlineWebSmokeReportServiceImpl implements OnlineWebSmokeReportService {
    @Autowired
    private OnlineWebSmokeReportMapper mapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    synchronized public int addReport(String tablename, OnlineWebSmokeReport report) {
        return mapper.insert(tablename, report);
    }

    @Override
    synchronized public List<OnlineWebSmokeReport> findAllReport(String tablename) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/AllReport/";
        redisTemplate.setKeySerializer(redisSerializer);
        List<OnlineWebSmokeReport> List = (List<OnlineWebSmokeReport>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = mapper.selectAllReport(tablename);
            redisTemplate.opsForValue().set(key, List, 120, TimeUnit.SECONDS);
            //   System.out.println(key + " 缓存中没找到，访问数据库！");
        }// else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return List;

    }

    @Override
    synchronized public List<OnlineWebSmokeReport> findByType(String tablename, String type) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/ReportByType/" + type;
        redisTemplate.setKeySerializer(redisSerializer);
        List<OnlineWebSmokeReport> List = (List<OnlineWebSmokeReport>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = mapper.selectByType(tablename, type);
            redisTemplate.opsForValue().set(key, List, 120, TimeUnit.SECONDS);
            //System.out.println(key + " 缓存中没找到，访问数据库！");
        } //else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return List;

    }

    @Override
    public int findMaxId(String tablaname) {
        return mapper.selectMaxId(tablaname);
    }

    @Override
    synchronized public List<OnlineWebSmokeReport> findReportByTime(String tablename, String start, String end, String version) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/From" + start + "to" + end + "/Version:" + version;
        redisTemplate.setKeySerializer(redisSerializer);
        List<OnlineWebSmokeReport> List = (List<OnlineWebSmokeReport>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = mapper.selectAllReportByTime(tablename, start, end, version);
            redisTemplate.opsForValue().set(key, List, 120, TimeUnit.SECONDS);
            //  System.out.println(key + " 缓存中没找到，访问数据库！");
        }// else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return List;

    }

    @Override
    public List<String> findAllVersion(String tablename) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/AllVersion";
        redisTemplate.setKeySerializer(redisSerializer);
        List<String> List = (List<String>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = mapper.selectAllVersion(tablename);
            redisTemplate.opsForValue().set(key, List, 120, TimeUnit.SECONDS);
            // System.out.println(key + " 缓存中没找到，访问数据库！");
        }
        // else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return List;
    }
}
